<?php 
$ubigeo = '';

$fecdoc = '';
$fecent = '';
$hoy = strftime('%Y-%m-%H');
$ocprov = '';
$ocmonto = '';
$ocldoc = '';
$orden = '';
$disable2 = '';
$itm_cnt = 0;
$buscar = false;
$find = false;
$combo_alm = '';
$row = '';
include('../Connections/KRAPPERCON.php');
$orden = '';
$combo = '';
include('../Query_Min/getAlmacenes.php');
$combo_alm = $combo;
//echo str_replace('h','xxx','hola');
//echo $combo;
if (isset($_POST['orden'])) {
$orden = $_POST['orden'];
$buscar = true;
}
if ($buscar){
	$sql_oc = "call sp_findOC(".$orden.");";
	$res = $mysqli->query($sql_oc) or die($mysqli->error.__LINE__);;
	if ($res){
		$find = true;
		while($row = $res->fetch_assoc()){
			if ($itm_cnt==0){
				$disable2 = "disabled";
			// Obtener Datos de cabecera
				$fecdoc = $row['OrdenFecDoc'];
				$fecent = $row['OrdenFecEnt'];
				$ocprov = $row['ProveedID'];
				$ocmonto = $row['OrdenMonto'];
				$ocldoc = $row['ClaDocID'];
			}
			$itm_cnt = $itm_cnt + 1;
			$oc_posn[] = $row['OrdenPos'];
			$oc_cods[] = $row['ArticuloID'];
			$oc_noms[] = $row['DetOrdTxt'];
			$oc_prun[] = $row['DetPreUM'];
			$oc_qtpe[] = $row['DetOrdQtp'];
			$oc_qtat[] = $row['DetOrdQta'];
			$oc_nets[] = $row['DetPreNet'];
			$oc_almc[] = $row['AlmacenID'];
			//$oc_cent[] = $row['CentroID'];
			$oc_dels[] = $row['DetOrdDel'];
		}
		$res->close();
		$mysqli->check_result();
	}
}
?>

<div>
  <input type="button" value="Nueva OC" onclick="nuevaOC();" />
  &nbsp;
  <input type="button" value="Editar" onclick="editarOC();" />
  &nbsp;
  <input type="button" value="Grabar" onClick="grabarOC()">&nbsp;<input type="button" value="Imprimir" onClick="imprimirOC()">&nbsp;<input type="button" value="Listado" onClick="listarOC()">&nbsp;<!--<input type="button" value="Verificar" onClick="verificarOC()">-->
</div>
  <table width="100%" border="0" cellspacing="0" id="tbl_dat_oc" cellpadding="0">
    <tr>
      <td>Clase de Documento :</td>
      <td>
        <select name="cb_oc_cdoc"  class='<?php echo $disable2; ?>' id="cb_oc_cdoc">
        <?php 
		$cladoc = 'OC';
		include('../Query_Min/getClaDocs.php');
		?>
        </select>
      </td>
      <td>Numero de Orden :</td>
      <td><span style="font-weight:bold" id="sp_nroord"></span></td>
    </tr>
    <tr>
      <td>Fecha Documento :</td>
      <td>
        <input name="t_fecdoc" type="text" id="t_fecdoc" size="15" maxlength="10" >
      </td>
      <td>Proveedor :</td>
      <td>
        <select name="cb_proveedor" class='<?php echo $disable2; ?>' id="cb_proveedor">
        <?php 			
		include('../Query_Min/getProveedores.php');
		 ?>
        </select>
      </td>
    </tr>
    <tr>
      <td>Fecha Entrega :</td>
      <td>
        <input type="text" name="t_fecent" id="t_fecent">
      </td>
      <td>Monto Total :</td>
      <td><span id="sp_monord"></span></td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
    <tr>
      <td colspan="4">
      <table width="100%" style="vertical-align:middle;" border="1" cellspacing="0" cellpadding="0" rules="all" id="tbl_itm_oc" class="tableIn">
      <thead>
        <tr>
          <th>Articulo</th>
          <th>Descripcion</th>
          <th>Precio Unitario</th>
          <th>Cantidad</th>
          <th>Neto</th>
          <th>Almac&eacute;n</th>
          <th>&nbsp;</th>
        </tr>
        </thead>
        <tbody>
         <?php 		 
		 $last = '';
		 if (!$find){
		$itm_cnt = 0;
		$disable= '';
			 for($a=0;$a<11;$a++){
				$itm_cnt = $itm_cnt + 1;
				$oc_posn[] = '';
				$oc_cods[] =  '';
				$oc_noms[] =  '';
				$oc_prun[] =  '';
				$oc_qtpe[] =  '';
				$oc_qtat[] =  '';
				$oc_nets[] =  '';
				$oc_almc[] =  '';
				//$oc_cent[] =  '';
				$oc_dels[] =  '';
			 }		 
		 
		 }
		 else {
		 $itm_cnt = $itm_cnt + 1;
		 }
//		 echo $itm_cnt;
		 for($a=0;$a < $itm_cnt ; $a++){
		 $hide = '';
		 $disable = '';
		 $mult = 1;
		 if ($a == ($itm_cnt-1)){
		 	$hide = 'display:none';
		 	$oc_posn[] = '';
			$oc_cods[] =  '';
			$oc_noms[] =  '';
			$oc_prun[] =  '';
			$oc_qtpe[] =  '';
			$oc_qtat[] =  '';
			$oc_nets[] =  '';
			$oc_almc[] =  '';
			//$oc_cent[] =  '';
			$oc_dels[] =  '';
		 }
		 else {
		 if ($oc_qtat[$a]>0){
		 $last = 'trLast';
		 $disable = 'disabled';
		 $mult = -1;
		 }
		 }
		 ?>
         
        <tr style="<?php echo $hide; ?>" title="<?php echo $oc_posn[$a]*$mult;?>" class="<?php echo $last ;?>">
          <td><input type="text" class="<?php echo $disable;?>" value="<?php echo $oc_cods[$a]; ?>" onchange="enterSP_Cod(this,event)" <?php echo $disable;?>></td>
          <td><span><?php echo $oc_noms[$a]; ?></span></td>
          <td>
          <input  type="text" class="<?php echo $disable;?>" onchange="calcOC1(this)" size="15" value="<?php echo $oc_prun[$a]; ?>" maxlength="10" <?php echo $disable;?> />
          </td>
          <td>
            <input  type="text" class="clsOCPre <?php echo $disable;?>" onchange="calcOC1(this)"  value="<?php echo $oc_qtpe[$a]; ?>" size="10" maxlength="8" <?php echo $disable;?>>
          </td>
          <td><span><?php echo $oc_nets[$a]; ?></span></td>
          <td><select class="<?php echo $disable;?>">
          <?php echo str_replace("value='".$oc_almc[$a]."'","value='".$oc_almc[$a]."' selected='selected'",$combo_alm);   ?>
          </select></td>
          <td><input type="button" title="Quitar Item" value="X" <?php if ($disable=='') echo 'onclick="delPos_OC(this)"'; ?> /></td>
        </tr>
        <?php 
		}
		?>
        </tbody>
        <tfoot>
        <tr>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td>&nbsp;</td>
          <td><input name="btn_addnew" type="button" value="Agregar" onclick="addPos()" /></td>
          <td>&nbsp;</td>
        </tr>
       </tfoot>
      </table>
        </td>
      </tr>
    <tr>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
      <td>&nbsp;</td>
    </tr>
  </table>

<script type="text/javascript">

function nuevaOC(){
/*	$('#sp_nroord,#sp_monord').html('');
	GEBI('cb_oc_cdoc').selectedIndex = 0;
	GEBI('cb_proveedor').selectedIndex = 0;		
	$('#tbl_dat_oc input[type|=text]').val('');
	editarOC();
	$('#tbl_itm_oc tbody tr').not(':last').remove();
	addPos();	*/
	loadPage('Procesos/MantOC.php');
}

function grabarOC(){
	var items = "";
	var orden = $('#sp_nroord').html();	
	if ($.trim(orden)== ""){
	// Grabar OC	
			$('#tbl_itm_oc tbody tr').each(function(){
				var fila = $(this);
				var row = fila.find('td:first');		
				var cod = row.find('input').val();
				if ($.trim(cod)!="" && cod !=null){
				var txt = row.next().find('span').html();
				var pru = row.next().next().find('input').val(); 
				var qty = row.next().next().next().find('input').val();				
				var alm = row.next().next().next().next().next().find('select').val();				
				items = items + cod + '|' + pru + '|' + qty + '|' + txt + '|' + alm + "#";
				}
				else {
				if (!fila.hasClass('trLast')){
					fila.remove();
				}
				}
			});
	///-----------------
		sendData('Procesos/Opers/OperOC.php',
		{
		oper : 'add',
		cb_oc_cdoc : GEBI('cb_oc_cdoc').value,
		t_fecent : GEBI('t_fecent').value,
		t_fecdoc : GEBI('t_fecdoc').value,
		cb_proveedor : GEBI('cb_proveedor').value,	
		items : items
		});
	}
	else {
	// Actualizar OC
	$('#tbl_itm_oc tbody tr').each(function(){
				var fila = $(this);
				var posn = fila.attr('title');
				var no_send = false;
				if (posn == null || $.trim(posn) == '') {
				posn = 0; //Indica que es posicion nueva
				}
				if (posn < 0){
				no_send = true;
				posnr = Math.abs(posn);
				}
				if (fila.hasClass('deleted')){
				items = items + posn + '|' + 'DELETED#' 
				fila.addClass('rowDel');
				}
				else if (!no_send) {
					var row = fila.find('td:first');		
					var cod = row.find('input').val();
					if ($.trim(cod)!="" && cod !=null){
						var txt = row.next().find('span').html();
						var pru = row.next().next().find('input').val(); 
						var qty = row.next().next().next().find('input').val();	
						var alm = row.next().next().next().next().next().find('select').val();				
						items = items + posn + '|' + cod + '|' + pru + '|' + qty + '|' + txt + '|' + alm + "#";
					}
					else {
						if (!fila.hasClass('trLast')){
						fila.remove();
						}
					}
				}				
			});
			// Enviar Info al Servidor
		sendData('Procesos/Opers/OperOC.php',
		{
		oper : 'upd',
		orden : orden ,
		cb_oc_cdoc : GEBI('cb_oc_cdoc').value,
		t_fecent : GEBI('t_fecent').value,
		t_fecdoc : GEBI('t_fecdoc').value,
		cb_proveedor : GEBI('cb_proveedor').value,	
		items : items
		});
	}
}

function imprimirOC(){
}

function addPos(){
var newrow = $('#tbl_itm_oc tbody tr:last').clone().css('display','');
newrow.find('input[type|=text]').val('');
$('#tbl_itm_oc tbody').append(newrow);
}

function enterOC_Cod(obj,e){
//if (e.keyCode == 13){
	if ($.trim(obj.value) == ''){
	$(obj).parent().next().find('span').html('');
	return;
	}
	sendData('Query_Min/getDatArticulo.php',{t_codart : obj.value , field : 'venta'},function(){
		 var mobj  = eval('(' + $('#d_result').html() + ')');
		 $(obj).parent().next().find('span').html(mobj.desc);
		 obj.value = mobj.code;
	});
	//}
}

function listarOC(){
	loadPage('Procesos/Listas/LstOrdCom.php');
}

function delPos_OC(obj){
	var rows = $('#tbl_itm_oc tbody tr').length;
	if (rows > 1){
	$(obj).parent().parent().hide().addClass('deleted');
	}
}

function editarOC(){
	$('#tbl_dat_oc').enable();
}

function noEditOC(){
	$('#tbl_dat_oc *').disable();
}

/*function calcSubt(tr){	
	if (tr==null) {
	tr = '#tbl_itm_oc tr';
	}
	$(tr).each(function(){
		var fila = $(this);
		var td = fila.find('td:first');
		var cod = fila.find('input').val();
		if ($.trim(cod)!=""){
		var pru =   pFloat(td.next().next().find('input').val());
		var ctd = 	pInt(td.next().next().next().find('input').val());
		var neto =   myRound(ctd * pru);
//		alert(neto);
		td.next().next().next().next().find('span').html(neto);		
		}
	});
}*/

function calcOC1(obj){
// recibe cantidad
	var monto = 0;
	var a = $('.clsOCPre').each(function(){
		var ctd = pInt(this.value);
		var pre = pFloat($(this).parent().prev().find('input').val());
		var neto = myRound(ctd*pre);
		$(this).parent().next().find('span').html(neto);
		//alert(neto);
		monto += neto;
	});
			$('#sp_monord').html(monto);
	
}

/*function calcOC2(obj){
// recibe precio
var ctd = obj.value
var pre = parseFloat($(obj).next().find('input').val());
var neto = myRound(ctd*pre);
$(obj).next().next().find('span').html(neto);
}*/

function verificarOC(){
	//calcSubt();
}

<?php
if ($buscar){
	if ($find){
	?>
	$('#sp_nroord').html('<?php echo $orden; ?>');
	GEBI('cb_oc_cdoc').value=('<?php echo $ocldoc; ?>');
	GEBI('t_fecdoc').value=('<?php echo $fecdoc ?>');
	GEBI('t_fecent').value=('<?php echo $fecent; ?>');
	$('#sp_monord').html('<?php echo $ocmonto; ?>');
	GEBI('cb_proveedor').value=('<?php echo $ocprov; ?>');
	//$('#tbl_itm_oc tbody').html();
	noEditOC();
	<?php 
	}
	else {
	?>
	showOk('Documento de Compra <?php echo $orden ?> no existe');
	<?php 
	}
}
else {
?>
GEBI('t_fecdoc').value = '<?php echo $hoy; ?>';
GEBI('t_fecent').value = '<?php echo $hoy; ?>';
<?php 
}
?>
</script>